// pages/allinstitutions/index.js
import urls from '../../utils/urls.js'
import loginFn from '../../utils/login.js'
var app = getApp()
Page({

	/**
	 * 页面的初始数据
	 */
	data: {
		page: 1,
		perpage: 20,
		totalPage: 1,
		actIdx: 0, //tab第几个
		searchtype: '全部类型',
		list: [],
		priorityIdx: 0,
		priority: [{
				'id': 1,
				'name': '距离优先',
				'orderBy': 'distance'
			},
			{
				'id': 2,
				'name': '好评优先',
				'orderBy': 'remark'
			},
			{
				'id': 3,
				'name': '效果优先',
				'orderBy': 'effect'
			},
			{
				'id': 4,
				'name': '服务优先',
				'orderBy': 'service'
			},
			{
				'id': 5,
				'name': '低价优先',
				'orderBy': 'low'
			},
			{
				'id': 6,
				'name': '高价优先',
				'orderBy': 'high'
			}
		],
		selects: false, //背景显示
		priorityshow: true, //隐藏优先选择框
		location: [{
			"districtId": 0,
			"districtName": "附近"
		}],
		locationshow: true, //隐藏附近选择框
		locationIdx: 0, //选择了第几个选择框
		allshow: true, //隐藏所有类型
		alltype: [{
				'id': 0,
				'name': '全部'
			},
			{
				'id': 1,
				'name': '小学辅导'
			},
			{
				'id': 2,
				'name': '初中辅导'
			},
			{
				'id': 3,
				'name': '高中辅导'
			}
		],
		courseMainId: 0, //课程id
		courseMains: [], //课程类型
		courseMainIdx: 0, //主类课程
		courseSub: 0, //子课程
		keyword: '', //关键词
		districeId: 0, //城市id
		orderBy: 'distance' //优先

	},
	searchtxt: '', //关键词

	search: function(options) {
		var that = this;
		var loginInfo = loginFn()
		loginInfo.courseMainId = this.data.courseMainId > 0 ? this.data.courseMainId : '';
		if (this.data.totalPage >= this.data.page) {
			loginInfo.page = this.data.page + 1
			wx.request({
				url: urls.courseall,
				method: 'POST',
				data: loginInfo,
				success: function(res) {

					if (res.data.code == '0') {
						var lists = that.data.list
						lists = lists.concat(res.data.data.list)
						console.log(lists, 'kobekobe')
						that.setData({
							list: lists,
							page: that.data.page + 1
						})
					}
				}
			})
		}
	},

	/**
	 * 生命周期函数--监听页面加载
	 */
	onLoad: function(options) {
		console.log('options:',options);
		var that = this;
		wx.getSystemInfo({
			success(res) {
				that.setData({
					winheight: res.windowHeight
				})
			}
		})
		for (var i = 0; i < app.globalData.classtype.courseMain.length; i++) {
			if (app.globalData.classtype.courseMain[i].courseMainId == options.courseMainId) {
				this.setData({
					searchtype: app.globalData.classtype.courseMain[i].courseMain
				})
			}
		}
		this.setData({
			courseMains: app.globalData.classtype.courseMain
		})
		if (options.courseMainId) {
			this.setData({
				courseMainId: options.courseMainId,

			})
			this.getcourseFn(options)
		};
		if (options.keyword) {
			this.searchtxt = options.keyword,
				this.setData({
					keyword: options.keyword
				})
			this.seachekeyword()
		};

		if (!options.keyword) {
			this.search(options)
		}


	},

	/**
	 * 生命周期函数--监听页面初次渲染完成
	 */
	onReady: function() {

	},

	/**
	 * 生命周期函数--监听页面显示
	 */
	onShow: function() {
		console.log(this.data.courseMains)

	},

	/**
	 * 生命周期函数--监听页面隐藏
	 */
	onHide: function() {

	},

	/**
	 * 生命周期函数--监听页面卸载
	 */
	onUnload: function() {

	},

	/**
	 * 页面相关事件处理函数--监听用户下拉动作
	 */
	onPullDownRefresh: function() {

	},

	/**
	 * 页面上拉触底事件的处理函数
	 */
	onReachBottom: function() {
		var that = this;
		var loginInfo = loginFn()
		loginInfo.courseMainId = this.data.courseMainId > 0 ? this.data.courseMainId : '';
		loginInfo.lat = app.globalData.location.latitude
		loginInfo.lng = app.globalData.location.longitude
		if (this.data.courseSub > 0) {
			loginInfo.courseSub = this.data.courseSub
		};
		if (this.data.districeId) {
			loginInfo.districeId = this.data.districeId
		};
		if (this.data.orderBy) {
			loginInfo.orderBy = this.data.orderBy
		};
		if (this.searchtxt !== '') {
			loginInfo.keyword = this.searchtxt
		};
		if (this.data.totalPage > this.data.page) {
			loginInfo.page = this.data.page + 1
			wx.request({
				url: urls.courseall,
				method: 'POST',
				data: loginInfo,
				success: function(res) {
					console.log(res, 'kobekobe')
					if (res.data.code == '0') {
						var lists = that.data.list
						lists = lists.concat(res.data.data.list)

						that.setData({
							list: lists,
							page: that.data.page + 1
						})
					}

				}
			})
		};

	},

	/**
	 * 用户点击右上角分享
	 */
	onShareAppMessage: function() {

	},
	allshow: function() {
		this.setData({
			locationshow: !this.data.locationshow
		})
	},
	nearFn: function(e) { //附近
		var locationshow = !this.data.locationshow
		this.setData({
			selects: !locationshow,
			locationshow: locationshow,
			allshow: true,
			priorityshow: true,
			actIdx: e.currentTarget.dataset.idx
		})
	},
	allFn: function(e) { //全部类型
		var allshow = !this.data.allshow
		this.setData({
			selects: !allshow,
			allshow: allshow,
			priorityshow: true,
			locationshow: true,
			actIdx: e.currentTarget.dataset.idx
		})
	},
	distanceFn: function(e) { //距离优先
		var priorityshow = !this.data.priorityshow
		this.setData({
			selects: !priorityshow,
			locationshow: true,
			allshow: true,
			priorityshow: priorityshow,
			actIdx: e.currentTarget.dataset.idx
		})
	},
	selpriorityFn: function(e) { //选择优先
		this.setData({
			priorityIdx: e.currentTarget.dataset.idx,
			selects: false,
			priorityshow: true,
			orderBy: this.data.priority[e.currentTarget.dataset.idx].orderBy
		});
		var that = this;
		var loginInfo = loginFn()
		loginInfo.courseMainId = this.data.courseMainId > 0 ? this.data.courseMainId : '';
		if (this.data.courseSub) {
			loginInfo.courseSub = this.data.courseSub
		};
		if (this.data.districeId) {
			loginInfo.districeId = this.data.districeId
		};
		if (this.searchtxt !== '') {
			loginInfo.keyword = this.searchtxt
		};
		loginInfo.orderBy = this.data.priority[e.currentTarget.dataset.idx].orderBy
		if (e.currentTarget.dataset.idx === 0) {
			wx.getLocation({
				type: 'gcj02',
				success(res) {
					loginInfo.lat = res.latitude
					loginInfo.lng = res.longitude
					wx.request({
						url: urls.courseall,
						method: 'POST',
						data: loginInfo,
						success: function(res) {
							if (res.data.code == '0') {
								that.setData({
									list: res.data.data.list
								})
							}
						}
					})
				}
			});
		} else {
			wx.request({
				url: urls.courseall,
				method: 'POST',
				data: loginInfo,
				success: function(res) {
					if (res.data.code == '0') {
						that.setData({
							list: res.data.data.list
						})
					}
				}
			})
		};

	},
	sellocationFn: function(e) { //选择地区
		this.setData({
			locationIdx: e.currentTarget.dataset.idx,
			selects: false,
			locationshow: true,
			districeId: e.currentTarget.dataset.id
		});
		var that = this;
		var loginInfo = loginFn()
		loginInfo.courseMainId = this.data.courseMainId > 0 ? this.data.courseMainId : '';
		if (this.data.courseSub) {
			loginInfo.courseSub = this.data.courseSub
		};
		if (this.data.orderBy) {
			loginInfo.orderBy = this.data.orderBy
		};
		if (this.searchtxt !== '') {
			loginInfo.keyword = this.searchtxt
		};
		loginInfo.lat = app.globalData.location.latitude
		loginInfo.lng = app.globalData.location.longitude
		if (e.currentTarget.dataset.idx > 0) {
			loginInfo.districeId = e.currentTarget.dataset.id
			wx.request({
				url: urls.courseall,
				method: 'POST',
				data: loginInfo,
				success: function(res) {
					if (res.data.code == '0') {
						that.setData({
							list: res.data.data.list
						})
					}

				}
			})
		} else {
			wx.getLocation({
				type: 'gcj02',
				success(res) {
					loginInfo.lat = res.latitude
					loginInfo.lng = res.longitude
					wx.request({
						url: urls.courseall,
						method: 'POST',
						data: loginInfo,
						success: function(res) {
							if (res.data.code == '0') {
								that.setData({
									list: res.data.data.list
								})
							}

						}
					})
				}
			});
		};



	},
	allchangeFn: function(e) { //全部改变
		this.setData({
			courseMainIdx: e.currentTarget.dataset.idx,
			selects: true,
		})
	},
	selectTypeFn: function(e) { //选择类型
		this.setData({
			selects: false,
			allshow: true
		})
		var that = this;
		var searchtype = this.data.courseMains[this.data.courseMainIdx].courseMain
		for (var i = 0; i < this.data.courseMains[this.data.courseMainIdx].sub.length; i++) {
			console.log(this.data.courseMains[this.data.courseMainIdx].sub[i].courseSubId)
			if (e.currentTarget.dataset.id == this.data.courseMains[this.data.courseMainIdx].sub[i].courseSubId) {
				searchtype = this.data.courseMains[this.data.courseMainIdx].sub[i].courseSub
			}
		}

		this.setData({
			searchtype: searchtype,
			courseSub: e.currentTarget.dataset.id,
			courseMainId: this.data.courseMains[this.data.courseMainIdx].courseMainId
		})
		var loginInfo = loginFn()
		loginInfo.lat = app.globalData.location.latitude
		loginInfo.lng = app.globalData.location.longitude
		loginInfo.courseMainId = this.data.courseMains[this.data.courseMainIdx].courseMainId
		loginInfo.courseSub = e.currentTarget.dataset.id
		if (this.data.orderBy) {
			loginInfo.orderBy = this.data.orderBy
		};
		if (this.data.districeId) {
			loginInfo.districeId = this.data.districeId
		};
		if (this.searchtxt !== '') {
			loginInfo.keyword = this.searchtxt
		};
		wx.request({
			url: urls.courseall,
			method: 'POST',
			data: loginInfo,
			success: function(res) {
				if (res.data.code == '0') {
					that.setData({
						list: res.data.data.list,
						totalPage: res.data.data.totalPage,
						page: 1
					})
				}

			}
		})


	},
	getcourseFn: function(options) { //获取搜索课程
		var that = this;
		var loginInfo = loginFn()
		loginInfo.courseMainId = options.courseMainId
		loginInfo.lat = app.globalData.location.latitude
		loginInfo.lng = app.globalData.location.longitude
		wx.request({
			url: urls.courseall,
			method: 'POST',
			data: loginInfo,
			success: function(res) {
				if (res.data.code == '0') {
					var locations = res.data.data.district
					locations.unshift({
						"districtId": 0,
						"districtName": "附近"
					})
					that.setData({
						list: res.data.data.list,
						location: locations,
						totalPage: res.data.data.totalPage
					})
				}
			}
		})
	},
	// 点击空白位置全部消失
	selectAll: function() {
		this.setData({
			selects: false,
			locationshow: true,
			allshow: true,
			priorityshow: true
		})
	},
	course_serchFn: function(e) { //搜索输入
		this.searchtxt = e.detail.value
	},
	seachekeyword: function(e) { //搜索
		console.log('seachekeyword');
		var that = this;
		this.setData({
			selects: false,
			locationshow: true,
			allshow: true,
			priorityshow: true
		})
		var loginInfo = loginFn()
		loginInfo.keyword = this.searchtxt
		loginInfo.lat = app.globalData.location.latitude
		loginInfo.lng = app.globalData.location.longitude
		if (this.data.courseMainId > 0) {
			loginInfo.courseMainId = this.data.courseMainId
		};
		if (this.data.courseSub) {
			loginInfo.courseSub = this.data.courseSub
		};
		if (this.data.orderBy) {
			loginInfo.orderBy = this.data.orderBy
		};
		if (this.data.districeId) {
			loginInfo.districeId = this.data.districeId
		};
		if (this.searchtxt !== '') {
			loginInfo.keyword = this.searchtxt
		};
		wx.request({
			url: urls.courseall,
			method: 'POST',
			data: loginInfo,
			success: function(res) {
				if (res.data.code == '0') {
					var locations = res.data.data.district
					locations.unshift({
						"districtId": 0,
						"districtName": "附近"
					})
					that.setData({
						list: res.data.data.list,
						totalPage: res.data.data.totalPage,
						page: 1,
						location: locations
					})
				}

			}
		})
	}
})
